home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!usenet
- From: staley@canary.lanl.gov (Martin Staley)
- Newsgroups: comp.lang.c++
- Subject: Compiler unable to comprehend template for "<<" operator. What's wrong???
- Date: 10 Mar 1996 05:07:01 GMT
- Organization: Los Alamos National Laboratory
- Distribution: world
- Message-ID: <4hto1l$bvl@newshost.lanl.gov>
- NNTP-Posting-Host: canary.lanl.gov
-
-
- Consider the following code:
-
- #include <iostream.h>
-
- template<class Type>
- class vector {
- Type *v;
- int size;
- friend ostream &operator<<(ostream &, vector<Type> &);
- };
-
- template<class Type>
- ostream &operator<<(ostream &ostr, vector<Type> &vec) {
- }
-
- main() {
- vector<float> v;
- cout << v;
- }
-
- This is an extremely simplified version of some stuff I'm writing. But
- it will not compile. Both g++ (for the Suns) and Sun c++ give me the
- following error:
-
- ld: Undefined symbol
- ___ls__FR7ostreamRt6vector1Zf
-
- Apparently the compiler doesn't know how to use the template for
- operator<< to instantiate the << operator for a vector<float>. WHAT'S
- WRONG HERE??? I've tried several minor variations to the above code,
- such as saying "class vector<Type> &vec" instead of "vector<Type> &vec"
- as the second formal-parameter declaration for the operator<< function.
- I've tried it with and without reference arguments. But nothing seems
- to work. I've also tried writing operator<< as just a regular function
- instead of a friend of class vector (though I really want it to be a
- friend). *Then* I get:
-
- In function `int main()':
- no conversion from `vector<float>' to type with default
- `operator <<'
-
- Again, the compiler seems unable to build the necessary operator<<.
- Things work fine if I write operator<< specifically for floats:
-
- ostream &operator<<(ostream &ostr, vector<float> &vec) {
- ...
- }
-
- But if I'm required to do this for every different "vector" type that
- I use, then it sort of defeats the purpose of using templates!
-
- Can anybody tell me what, if anything, that I am doing wrong above?
-
- Martin staley@cnls.lanl.gov
-
-